package com.all.play.mymd.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.all.play.mymd.R;
import com.all.play.mymd.adapter.RefreshAdapter;
import com.all.play.mymd.animtion.RefreshItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * https://www.jianshu.com/p/ce347cf991db
 * https://www.cnblogs.com/liunanjava/p/5860024.html
 * https://github.com/ln0491/SwipeRefreshLayoutRecyclerViewDemo
 * https://blog.csdn.net/u011374875/article/details/51744448
 * https://blog.csdn.net/abc6368765/article/details/52557145
 * https://www.jianshu.com/p/d23b42b6360b
 */
public class FirstActivity extends AppCompatActivity {


    private RecyclerView first_rv;
    private SwipeRefreshLayout first_srl;
    List<String> mDatas = new ArrayList<>();
    private RefreshAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        //初始化view
        initView();
        //加载数据
        initData();
        //下拉和上拉的监听
        initListener();
    }




    private void initView() {
        first_srl = (SwipeRefreshLayout) findViewById(R.id.first_srl);
        first_rv = (RecyclerView) findViewById(R.id.rv_first);

        //设置加载时的大小
        first_srl.setSize(SwipeRefreshLayout.DEFAULT);


        //设置加载条的颜色切换
        first_srl.setColorSchemeColors(ContextCompat.getColor(this,R.color.a),
                ContextCompat.getColor(this,R.color.b),
                ContextCompat.getColor(this,R.color.c));
        //设置加载时的背景色
        first_srl.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(this,R.color.srlbg));
        //下拉多少距离开始刷新
        first_srl.setDistanceToTriggerSync(100);
    }

    /**
     * 设置数据
     */
    private void initData() {

        for (int i = 0; i < 20; i++) {

            mDatas.add(" Item "+i);
        }
        //设置布局管理器
        initRecylerView();
    }

    /**
     * 设置布局管理器
     */
    private void initRecylerView() {

        adapter = new RefreshAdapter(this, mDatas);


        //添加动画
        first_rv.setItemAnimator(new DefaultItemAnimator());

        //添加分割线
        first_rv.addItemDecoration(new RefreshItemDecoration(this,RefreshItemDecoration.VERTICAL_LIST));



        //第三个参数是  排列顺序  true 数据倒序,false数据正序
        first_rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        first_rv.setAdapter(adapter);
    }

    private void initListener() {

        initPullListener();


        initLoadMoreListen();
    }


    /**
     * 下拉的监听
     */
    private void initPullListener() {

        //swiperefreshLayout  下拉刷新的监听
        first_srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                //模拟子线程中加载数据
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //设置假数据
                        List<String> headDatas=new ArrayList<String>();
                        for (int i=20;i<30;i++){
                            headDatas.add("HeadItem "+i);
                        }
                        //下拉时添加在列表的头部
                        adapter.addHeadItem(headDatas);
                        //停止刷新
                        first_srl.setRefreshing(false);
                        Toast.makeText(FirstActivity.this, "下拉加载数据", Toast.LENGTH_SHORT).show();
                    }
                },3000);//注意,是在30000ms中完成

            }
        });
    }

    /**
     * 上拉加载
     */
    private void initLoadMoreListen() {
        //RecycleView的滚动监听  滚动到最后一个条目时进行设置
        first_rv.addOnScrollListener(new RecyclerView.OnScrollListener() {

            int lastVisibleItem;//列表最后显示出来的条目

            @Override//滚动状态改变时
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //只要最后一个条目滑动出来就
                //设置正在加载的状态
                adapter.changeMoreState(adapter.LOADING_MORE);


                //newState判断RecyclerView的状态 是空闲时，
                // 同时，是最后一个可见的ITEM时才加载
                if (newState==RecyclerView.SCROLL_STATE_IDLE&&
                        lastVisibleItem+1==adapter.getItemCount()){
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                          List<String> footerDatas=new ArrayList<String>();
                            for (int i=0;i<10;i++){
                                footerDatas.add("foot item "+i);
                            }
                            //添加数据到列表的底部
                            adapter.addFootItem(footerDatas);

                            //设置回到上拉加载状态
                            adapter.changeMoreState(adapter.PULLUP_LOAD_MORE);

                            //没有加载更多了
                            //mRefreshAdapter.changeMoreStatus(mRefreshAdapter.NO_LOAD_MORE);
                            Toast.makeText(FirstActivity.this, "上拉更新了"+footerDatas.size()+"条数据", Toast.LENGTH_SHORT).show();

                        }
                    },3000);
                }
            }

            @Override//滚动时
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutmanager= (LinearLayoutManager) first_rv.getLayoutManager();
                lastVisibleItem=layoutmanager.findLastVisibleItemPosition();
            }
        });
    }
}
